<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/24 0024
 * Time: 13:21
 */

namespace app\admin\controller\shop;

use app\admin\model\WarehouseGeneralSku;
use app\common\model\ProductSpell as ProductSpellService;
use app\Validate\PageMustValidate;
use think\Request;

/**
 * 拼团后台方法类
 * Class ProductSpell
 * @package app\admin\controller\shop
 */
class ProductSpell{
    /**
     * 获取拼团商品详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProductSpell(Request $request){
        $post = $request->post();
        if(!isset($post["id"])){
            return writeJson(200,"缺少id","缺少id",10000);
        }
        $productSpellService = new ProductSpellService();
        $where["id"] = $post["id"];
        $field = 'id,cover_img,spell_name,product_id,batch_num,spell_price,spell_num,is_show,FROM_UNIXTIME(start_time) as start_time,FROM_UNIXTIME(end_time) as end_time';
        $model = $productSpellService->field($field)->where($where)->whereNull("delete_time")->find();
        if($model == null){
            return writeJson(200,"当前拼团商品不存在","当前拼团商品不存在",10000);
        }
        $model = $model->toArray();
        //处理数据
        return writeJson(200,$model);
    }

    /**
     * [新增/修改]拼团商品
     * product_id,general_sku_id,out_name 必传
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateProductSpell(Request $request){
        $post = $request->post();
        if(!isset($post["product_id"])){
            return writeJson(200,"缺少商品ID","缺少商品ID",10000);
        }
        if(!isset($post["spell_name"])){
            return writeJson(200,"缺少清仓名称","缺少清仓名称",10000);
        }

        //数据处理
        $new = array(
            "cover_img"=>isset($post["cover_img"]) ? $post["cover_img"] : "",
            "spell_name"=>$post["spell_name"],
            "product_id"=>$post["product_id"],
            "batch_num"=>isset($post["batch_num"]) ? $post["batch_num"] : 0,
            "spell_price"=>isset($post["spell_price"]) ? $post["spell_price"] : 0,
            "spell_num"=>isset($post["spell_num"]) ? $post["spell_num"] : 0,
            "is_show"=>isset($post["is_show"]) ? $post["is_show"] : 0,
            "start_time"=>isset($post["start_time"]) ? $post["start_time"] : time(),
            "end_time"=>isset($post["end_time"]) ? $post["end_time"] : time()
        );

        $productSpellService = new ProductSpellService();
        if(!isset($post["id"])){
            //新增
            $new["create_time"] = time();
            $res = $productSpellService->save($new);
        }else{
            //修改
            $editWhere["id"] = $post['id'];
            $res = $productSpellService->where($editWhere)->update($new);
        }

        if(!$res){
            return writeJson(200,"操作失败","操作失败",10000);
        }
        return writeJson(200,[]);
    }

    /**
     * 删除拼团商品
     * @param Request $request
     * @return \think\response\Json
     */
    public function deleteProductSpell(Request $request){
        $post = $request->post();
        if(!isset($post["id"])){
            return writeJson(200,"缺少关键参数","缺少关键参数",10000);
        }
        //修改
        $productSpellService = new ProductSpellService();
        $res = $productSpellService->where(["id"=>$post['id']])->setField("delete_time",time());
        if(!$res){
            return writeJson(200,"操作失败","操作失败",10000);
        }
        return writeJson(200,[]);
    }

    /**
     * 获取拼团商品列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     * @throws \think\exception\DbException
     */
    public function getProductSpellList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        if(isset($post['name'])){
            $where[] = ["om_product_spell.spell_name","like",'%'.$post['name'].'%'];
        }
        if(isset($post['international_code'])){
            $where[] = ["om_product.international_code","like",'%'.$post['international_code'].'%'];
        }

        /*
         * select
            om_product_out.id,
            om_product_out.cover_img,
            om_product_out.out_name,
            om_product_out.product_id,
            om_product_out.batch_num,
            om_product_out.out_price,
            om_product_out.general_sku_id,
            om_product_out.is_show,
            FROM_UNIXTIME(om_product_out.start_time) as start_time,
            FROM_UNIXTIME(om_product_out.end_time) as end_time
            from om_product_out
            inner join om_product on om_product_out.product_id = om_product.id
            where om_product.international_code like '%56476%'
         */
        $productSpellService = new ProductSpellService();
        $field = 'om_product_spell.id,
            om_product_spell.cover_img,
            om_product_spell.spell_name,
            om_product_spell.product_id,
            om_product_spell.batch_num,
            om_product_spell.spell_price,
            om_product_spell.spell_num,
            om_product_spell.is_show,
            om_product.product_name,
            FROM_UNIXTIME(om_product_spell.start_time) as start_time,
            FROM_UNIXTIME(om_product_spell.end_time) as end_time';
        $list = $productSpellService->field($field)->where($where)
            ->join("om_product","om_product_spell.product_id = om_product.id")
            ->whereNull("om_product_spell.delete_time")
            ->order("om_product_spell.create_time")
            ->paginate($post['page_number'], false, ['page' => $post['page']]);
        return writeJson(200,$list);
    }
}